<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-08-22
 * Time: 21:49
 */

namespace plugin\csauto\app\service\downloader;


use plugin\csauto\app\model\DownloaderModel;
use plugin\csauto\app\model\TorrentReleaseModel;
use plugin\csauto\app\service\QBittorrentService;
use plugin\csauto\app\service\SiteWorkService;
use plugin\csauto\app\service\SystemService;
use function Sodium\add;

class TorrentTaskService
{
    public static function checkTorrentStatus($taskItem)
    {
        /*开始check 一下所有的release的状态*/
        $torrentStatus = true;
        $torrentFiles = array();
        $torrentFiles[] = $taskItem['torrent_path'];
        $torrents = array();
        $torrents[] = $taskItem['torrent_hash'];
//        echo "taskItem {$taskItem->id} 1" . PHP_EOL;
        $torrentList = TorrentReleaseModel::where('task_id', $taskItem->id)->get();
        foreach ($torrentList as $item) {
            $torrents[] = $item['torrents_hash'];
            $torrentFiles[] = $item['torrents_path'];
            if (!(strtotime($item->updated_at) + SystemService::getConfig(SystemService::TASK_SEED_RETENTION_TIME) * 3600 < time())) {
                /*这里是没到设定删除时间的种子信息，如果不是99则标记未完成*/
                if ($item->status != 99) $torrentStatus = false;/*尚未标记未99的种子默认标记*/
            }

        }
//        echo "taskItem {$taskItem->id} 2" . PHP_EOL;
        if ($torrentStatus) {
            /*开始执行清理操作*/
            /*开始清理种子文件*/
//            return $torrents;
//            echo "taskItem {$taskItem->id} 3" . PHP_EOL;
            deleteFilesIfExists($torrentFiles);
            
            /*检查下载器是否存在*/
            $downloaderExists = DownloaderModel::where('id', $taskItem->downloader_id)->exists();
            
            if (!$downloaderExists) {
                /*下载器已被删除，直接删除任务，不操作QB*/
                $taskItem->status = 99;
                $taskItem->save();
                addlog('QB删除种子', "taskId:{$taskItem->id},下载器已被删除，直接清理任务");
                return true;
            }
            
            /*删除QB的文件*/
            $qb = QBittorrentService::createService($taskItem->downloader_id);
            if ($qb) {
                $qb->deleteTorrent($torrents, true);
                $taskItem->status = 99;
                $taskItem->save();
                addlog('QB删除种子', "taskId:{$taskItem->id},QB处理完成");
                return true;
            } else {
                addlog('QB删除种子', "taskId:{$taskItem->id},QB连接失败");
            }
        }
        return false;
    }

    public static function checkTorrentSurviveStatus($taskItem)
    {
        if (strtotime($taskItem->created_at) + SystemService::getConfig(SystemService::TASK_SEED_SURVIVE_TIME) * 3600 < time()) {
            $torrentFiles = array();
            $torrentFiles[] = $taskItem['torrent_path'];
            $torrents = array();
            $torrents[] = $taskItem['torrent_hash'];
            $torrentList = TorrentReleaseModel::where('task_id', $taskItem->id)->get();
            foreach ($torrentList as $item) {
                $torrents[] = $item['torrents_hash'];
                $torrentFiles[] = $item['torrents_path'];
                if ($item->status != 99) {
                    $item->status = 99;
                    $item->save();
                }
            }
            /*检查一下任务*/
            deleteFilesIfExists($torrentFiles);
            
            /*检查下载器是否存在*/
            $downloaderExists = DownloaderModel::where('id', $taskItem->downloader_id)->exists();
            
            if (!$downloaderExists) {
                /*下载器已被删除，直接删除任务，不操作QB*/
                $taskItem->status = 99;
                $taskItem->save();
                addlog('QB删除种子', "taskId:{$taskItem->id},下载器已被删除，直接清理任务(存活时间检查)");
                return true;
            }
            
            $qb = QBittorrentService::createService($taskItem->downloader_id);
            if ($qb) {
                $qb->deleteTorrent($torrents, true);
                $taskItem->status = 99;
                $taskItem->save();
                return true;
            }
        }
    }

    public static function checkTorrentDownloadStatus($taskItem)
    {
        if (strtotime($taskItem->updated_at) + SystemService::getConfig(SystemService::TASK_SEED_DOWNLOAD_TIME) * 3600 < time()) {
            /*触发清理机制*/
            $torrentFiles = array();
            $torrentFiles[] = $taskItem['torrent_path'];
            $torrents = array();
            $torrents[] = $taskItem['torrent_hash'];
            deleteFilesIfExists($torrentFiles);
            
            /*检查下载器是否存在*/
            $downloaderExists = DownloaderModel::where('id', $taskItem->downloader_id)->exists();
            
            if (!$downloaderExists) {
                /*下载器已被删除，直接删除任务，不操作QB*/
                $taskItem->status = 99;
                $taskItem->save();
                addlog('QB删除种子', "taskId:{$taskItem->id},下载器已被删除，直接清理任务(下载时间检查)");
                return true;
            }
            
            $qb = QBittorrentService::createService($taskItem->downloader_id);
            if ($qb) {
                $qb->deleteTorrent($torrents, true);
                $taskItem->status = 99;
                $taskItem->save();
                return true;
            }
        }
        return false;
    }
}